<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IFWiki.Models.GamePlace>" %>
<%@ Import Namespace="IFWiki.Utils"  %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
	<title>Go</title>
	<Style type="text/css">
    <%= Html.Encode(Model.GameStyle) %>
    </Style>
  <script type="text/javascript" src="../../Scripts/IfWiki.Cookies.js"></script>    
  <script type="text/javascript" src="../../Scripts/IfWiki.Player.js"></script>    
  <script type="text/javascript" src="../../Scripts/IfWiki.Write.js"></script>    
  <script type="text/javascript" src="../../Scripts/IfWiki.Place.js"></script>    
  <script type="text/javascript" src="../../Scripts/jquery-ui-1.7.2.custom.js"></script>
  <script type="text/javascript" src="../../Scripts/showdown.js"></script> 
  <link href="../../Content/Dialog.css" rel="stylesheet" type="text/css" />
		
  <script type="text/javascript">
    var player;
    player = new Object;
    var place;
    place = new Object;    
    var placeName;
    placeName = "<%= Html.Encode(Model.PlaceName) %>";
    
    $(document).ready(function() {
      
      //load player
      player = loadPlayer('player');

      if (!player) {
        alert('No player found. Game over.');
        //no player exists? go back to the welcome page for this game.
        //goto('');
        return;
      }
      if (player.vitals == undefined || player.vitals.life <= 0) {
        //play ends when the player's life force reaches 0 (or less)
        alert('Player found, but has no vitals or is dead.');
        //endplay();
        return;
      }
      
      place = loadPlace(placeName, player.gameToken, "<%= Model.PlaceItemsJson.ToSingleLine() %>");
      
		$('#dialog').dialog({
				autoOpen: false,
				width: 600,
				buttons: {
					"Cancel": function() { 
						$(this).dialog("close"); 
					} 
				}
			});


			$('#prompt').dialog({
				autoOpen: false,
				width: 600,
				buttons: {
					"Cancel": function() { 
					    prompt_callback();
						$(this).dialog("close"); 
					} ,
				    "OK": function() {
					    var answer = $('#answer').val();
						$(this).dialog("close");
						prompt_callback(answer);
												
					}					
				}
			});

            
        		//	$('#prompt').dialog('open');
                ask('what is your name?','Anon',usePlayerName);

      OnPlaceEnter(); //happens the same for every place in this game.
      InitPlace();    //specific to this place
      
        attachActions($('.noun'));
      
      savePlayer(player);
      savePlace(placeName, player.gameToken, place);
 
    });
    
    
    		function attachActions(jq_nouns) {
			jq_nouns.click(function(){
				$('#dialog').dialog('open');
				$('#ui-dialog-title-dialog').html(this.innerText);
				//create an li class=action for each action that this noun has.
				createList(this.innerText, $("#actionlist"));
				
				$('.action').click(function(){
					var action = this.innerText;
					var noun = $('#ui-dialog-title-dialog').html();
			        Apply(action,  noun);
					$('#dialog').dialog('close');
			        return false;
			    });
				return false;
			});			
		}
		
			
		function createList(noun, jquery_ul) {
			//clear the list...
		    jquery_ul.html("");
			//then add a list item for each action...
		    for (var action in place[noun]) {
				jquery_ul.append("<li class='action'>" + action + "</li>");
			}

		  //jquery_ul.append("<li class='action'>Punch the dog</li>");
		}	
		
		var prompt_callback = null;
		
		function ask(question, defaultAnswer, callback) {
			prompt_callback = callback;
			$('#prompt').dialog('open');
			$('#question').html(question);
			
		}

    
    function OnPlaceEnter() {
      <%= Model.OnPlaceEnterJS %>
    }
    
    //function InitPlace() {
    <%= Model.InitPlaceJS %>
    //}



    function usePlayerName(name) {
			if (name != undefined) {
				player.name = name;
			} else {
				player.name = "Anon Coward";
			}
			
			showPlayer(player);
		}
		    
  </script>  
</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="menu" runat="server">
        <div id="menucontainer">
            <ul id="menu">              
                <li><a class="current">Main</a></li>            
                <li><%= Html.ActionLink("Source", "ViewSource", "GamePlace", new { name=Model.Name}, null)%></li>
                <li><%= Html.ActionLink("Talk", "Talk", "Article", new { name = Model.Name, innerType = Model.InnerType }, null)%></li>
            </ul>
        </div>
    </div>
</asp:Content>


<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<style>
 <%= Model.PlaceStyle %>
</style>            

<div class='playerinfo'><span id='playerinfo'></span></div>
    <h2><%= Html.Encode(Model.PlaceName) %></h2>
    <div id='topMessages'></div>
    <p><%= Html.Encode(Model.DescriptionMarkdown).ToHtml() %></p>    
    <span id='messages'></span>        
    
  	<!-- ui-dialog -->
    <div id="dialog" title="Noun">
    	<ul id='actionlist'>
	    </ul>
	</div>
	
	<div id="prompt" title="Question...">
        <p id='question'>Question</p>
        <input type='text' id='answer' class='prompt_text' ></input>
	</div>

	
</asp:Content>

